<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 10px;
            margin-top: 10px;
            white-space: pre-wrap;
            max-height: 400px;
            overflow-y: auto;
        }
        .error {
            background: #f8d7da;
            border-color: #f5c6cb;
            color: #721c24;
        }
        .success {
            background: #d4edda;
            border-color: #c3e6cb;
            color: #155724;
        }
    </style>
</head>
<body>
    <h1>后端API连接测试</h1>
    
    <div class="test-section">
        <h3>1. 测试后端服务连接</h3>
        <button onclick="testConnection()">测试连接</button>
        <div id="connection-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 测试商品搜索API</h3>
        <button onclick="testProductSearch()">测试商品搜索</button>
        <div id="search-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>3. 测试商品类型API</h3>
        <button onclick="testProductTypes()">测试商品类型</button>
        <div id="types-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 测试品牌API</h3>
        <button onclick="testBrands()">测试品牌列表</button>
        <div id="brands-result" class="result"></div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8083';
        
        function showResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.textContent = JSON.stringify(data, null, 2);
            element.className = `result ${isError ? 'error' : 'success'}`;
        }
        
        async function testConnection() {
            try {
                const response = await fetch(`${API_BASE}/api/v1/product/search`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        keyword: '',
                        page: 1,
                        size: 1
                    })
                });
                
                if (response.ok) {
                    const data = await response.json();
                    showResult('connection-result', {
                        status: '连接成功',
                        statusCode: response.status,
                        data: data
                    });
                } else {
                    showResult('connection-result', {
                        status: '连接失败',
                        statusCode: response.status,
                        statusText: response.statusText
                    }, true);
                }
            } catch (error) {
                showResult('connection-result', {
                    status: '连接错误',
                    error: error.message
                }, true);
            }
        }
        
        async function testProductSearch() {
            try {
                const response = await fetch(`${API_BASE}/api/v1/product/search`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        keyword: '',
                        page: 1,
                        size: 8
                    })
                });
                
                const data = await response.json();
                showResult('search-result', data, !response.ok);
            } catch (error) {
                showResult('search-result', {
                    error: error.message
                }, true);
            }
        }
        
        async function testProductTypes() {
            try {
                const response = await fetch(`${API_BASE}/api/v1/product/types`);
                const data = await response.json();
                showResult('types-result', data, !response.ok);
            } catch (error) {
                showResult('types-result', {
                    error: error.message
                }, true);
            }
        }
        
        async function testBrands() {
            try {
                const response = await fetch(`${API_BASE}/api/v1/product/brands`);
                const data = await response.json();
                showResult('brands-result', data, !response.ok);
            } catch (error) {
                showResult('brands-result', {
                    error: error.message
                }, true);
            }
        }
        
        // 页面加载时自动测试连接
        window.onload = function() {
            testConnection();
        };
    </script>
</body>
</html>